<?php

/**
 * 
 * @author yuxun
 *
 */
class IosExamineCatService extends Service{
	
	public static function indexHeader(){
		return array(
				'id'         => array('text' => 'ID', 'rule' => 'int'),
				'name'       => array('text' => '分类名称'),
				'game_notice_url'       => array('text' => '游戏公告地址'),
				'game_sever_list_url'       => array('text' => '游戏服务器地址'),
				'login_server_address'       => array('text' => '登录服务器地址'),
				'login_server_port'       => array('text' => '登录服务器端口'),
				'resource_update_address' => array('text' => '资源更新地址'),
				'channel_platform_id' => array('text' => '资源渠道ID'),
				'create_time'     => array('text' => '添加时间'),
				'action'     => array('text' => '操作')
		);
	}
	
	public static function indexData($params){
		$modIosExamineCat = new ModIosExamineCat();
		$result = $modIosExamineCat->page($params);
		$length = count($result['data']);
		foreach($result['data'] as &$v){
			$type = 1;
			if($k == 0 && $length == 1){  //当前页只剩下一条数据时，删除之后需显示上一页的数据
				$type = 2;
			}

			$v['create_time'] = date('Y-m-d H:i:s', $v['create_time']);
			$v['action']  .= ' <a href="'.PubFunc::buildBackUrl('/oss/?r=iosExamineCat/edit').'&id='.$v['id'].'">编辑</a>';
			$v['action']  .= ' <a href="'.PubFunc::buildBackUrl('/oss/?r=iosExamineCat/delete',$type).'&id='.$v['id'].'" onclick="del()" confirm="是否要删除该条服信息?">删除</a>';
		}
		unset($v);
		return $result;
	}
	
	public static function doAdd($params){
		$user = AccAuth::me();
	
		$modIosExamineCat = new ModIosExamineCat();
		$modIosExamineCat->game = $params['g'];
		$modIosExamineCat->name  = $params['name'];
		$modIosExamineCat->create_user   = $user['UserID'];
		$modIosExamineCat->create_time   = time();
		$modIosExamineCat->update_user   = $user['UserID'];
		$modIosExamineCat->update_time   = time();

		$modIosExamineCat->game_notice_url   = $params['game_notice_url'];
		$modIosExamineCat->game_sever_list_url   = $params['game_sever_list_url'];
		$modIosExamineCat->login_server_address   = $params['login_server_address'];
		$modIosExamineCat->login_server_port   = $params['login_server_port'];
		$modIosExamineCat->resource_update_address   = $params['resource_update_address'];
		$modIosExamineCat->channel_platform_id   = $params['channel_platform_id'];
		$result = $modIosExamineCat->save();
		return array('state' => true, 'msg' => '成功增加');
	}
	
	public static function get($id){
		$modIosExamineCat = new ModIosExamineCat();
		$obj = $modIosExamineCat->findByPk($id);
	
		if($obj){
			$info = $obj->getAttributes();
			return $info;
		}else{
			return false;
		}
	}
	
	public static function doEdit($id, $params){
		if(empty($id)) return array('state' => false, 'msg' => '信息不存在');

		$user = AccAuth::me();
		$modIosExamineCat = new ModIosExamineCat();
		$update = array();
		$update['name'] = $params['name'];
		$update['game_notice_url'] = $params['game_notice_url'];
		$update['game_sever_list_url'] = $params['game_sever_list_url'];
		$update['login_server_address'] = $params['login_server_address'];
		$update['login_server_port'] = $params['login_server_port'];
		$update['resource_update_address'] = $params['resource_update_address'];
		$update['channel_platform_id'] = $params['channel_platform_id'];
		$result = $modIosExamineCat->updateByPk($id, $update);

		return true;
	}
	
	public static function delete($id){
		if(empty($id)) return false;

		$flag = IosExamineService::getForCategory($id);
		if($flag){
			return array('state' => false, 'msg' => '该分类已经被使用，不能删除！');
		}
		
		$modIosExamineCat = new ModIosExamineCat();
		$modIosExamineCat->deleteByPk($id);
		return array('state' => true, 'msg' => '成功删除');
	}
	
	
	/*
	 * 去重
	 */
	public static function repeat($id = 0, $field, $val,$g){
		$modIosExamineCat = new ModIosExamineCat();
		$conut = $modIosExamineCat->repeat($id, $field, $val,$g);
		return $conut;
	}
	
    /**
     * 获取列表
     * @return multitype:unknown
     */
	public static function getList(){
		$modIosExamineCat = new ModIosExamineCat();
		$params = array();
		$game = PubBase::getCurrentGame();
		$params['g'] = $game;
		$data = $modIosExamineCat->select($params);
		$list = array();
		foreach ($data as $key=>$val){
			$list[$val['id']] = $val['name'];
		} 
		return $list;
	}
	
	


}